//
//  HistogramViewCell.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/26.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "HistogramViewCell.h"
#import "HistogramView.h"
#import "NSString+StringSize.h"
@interface HistogramViewCell()
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *totalLabel;
@property (nonatomic,strong)HistogramView *histogramView;
@property (nonatomic,strong)UILabel *firstVerticallabel;
//@property (nonatomic,strong)UIView *maskView;

@end


@implementation HistogramViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    
    _titleLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:@"行驶里程分布"];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(24);
        make.top.mas_equalTo(self).offset(30);
    }];
    
    _totalLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:@"总计:   "];
    [self addSubview:_totalLabel];
    [_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(-24);
        make.top.mas_equalTo(self).offset(30);
    }];
    
    _histogramView = [HistogramView new];
    [self addSubview:_histogramView];
    [_histogramView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(173.0f/3);
        make.top.mas_equalTo(self.mas_top).offset(188.0f/3);
        make.right.mas_equalTo(self.mas_right).offset(-57/3.0f);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-145/3);
    }];
}
- (void)laodUIWithDataArr:(NSArray *)arr
{
    
    CGSize tempSize = [@"00" sizeWithLabelWidth:100 font:[UIFont systemFontOfSize:NormalFontSize]];
    CGFloat tempHeight = tempSize.height*0.5;
    if (arr.count == 0) {
        UILabel *centerLabel = [UILabel LabelWithFont:BigFontSize andColor:TitleColor andTextAlignment:center andString:@"无统计数据"];
        [self addSubview:centerLabel];
        [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        return;
    }
    // 线的路径
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(173.0f/3, 188.0f/3)];
    [linePath addLineToPoint:CGPointMake(173.0f/3, 261-143/3.0f)];
    [linePath addLineToPoint:CGPointMake(kScreenWidth - 57/3.0f,261-143/3.0f)];
    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineWidth = 0.5f;
    lineLayer.strokeColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f].CGColor;
    lineLayer.path = linePath.CGPath;
    lineLayer.fillColor = nil;
    [self.layer addSublayer:lineLayer];
    
    int maxNum = 0;
    int totalNum = 0;
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        totalNum += [dic[@"value"] intValue];
        if (maxNum<[dic[@"value"] intValue]) {
            maxNum = [dic[@"value"] intValue];
        }
    }
    _totalLabel.text = [NSString stringWithFormat:@"总计  %d",totalNum];
    int eachIntValu = maxNum/(4);
    NSString *str  = [NSString stringWithFormat:@"%d",eachIntValu];
    int eachValu =  ceil(maxNum*1.0f/(4)/pow(10,str.length-1))*pow(10,str.length-1);
    
    CGFloat lineVerticalLength = 261-143/3.0f - 188.0f/3;
    CGFloat lineHorizontalLength = kScreenWidth - 57/3.0f - 173.0f/3;
    
    CGFloat verticalLength = lineVerticalLength/(4+1.f);
    CGFloat horizontalLength = lineHorizontalLength/(arr.count);
    
    CGFloat x = 173.0f/3 ;
    CGFloat y = 261-143/3.0f;
    
    
    
    
    for (int i=0; i<5; i++) {
        UILabel *verticallabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:[NSString stringWithFormat:@"%d",eachValu*i]];
        [self addSubview:verticallabel];
        [verticallabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_left).offset(x-6);
            make.bottom.mas_equalTo(self.mas_top).offset(y-verticalLength*(i));
        }];
    }

    
    for (int j=0; j<arr.count; j++) {
        NSDictionary *dic = arr[j];
//        int num = [dic[@"value"] intValue];
//        CGFloat height = num*1.0f/eachValu*verticalLength;
        UILabel *horizontalLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:[NSString stringWithFormat:@"%@",dic[@"key"]]];
        [self addSubview:horizontalLabel];
        [horizontalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_left).offset(x+horizontalLength*(j+0.5));
            make.top.mas_equalTo(self.mas_top).offset(y+15);
        }];
    }
    
    [_histogramView loadHistogramWithArr:arr];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel.text = _title;
}
- (void)drawRect:(CGRect)rect
{
    
}

@end
